package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    //根据三级分类id，查询品牌列表集合
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //通过三级分类id，获取trademark_id
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);

        //List<Long> tmIdList = new ArrayList<Long>();
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            //for循环
            //for (BaseCategoryTrademark baseCategoryTrademark : baseCategoryTrademarkList) {
            //    tmIdList.add(baseCategoryTrademark.getTrademarkId());
            //}

            //Lambda表达式
            List<Long> tmIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                //获取品牌id
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());

            //根据品牌id，查询品牌数据
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(tmIdList);
            //返回数据
            return baseTrademarkList;
        }
        //默认返回
        return null;
    }

    //根据三级分类id，查询可选品牌列表集合
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //根据三级分类id，查询已经绑定好的品牌列表
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);

        //获取小米，苹果，华为的品牌id
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            List<Long> tmIdList = baseCategoryTrademarkList.stream().map((baseCategoryTrademark -> {
                //获取品牌id
                return baseCategoryTrademark.getTrademarkId();
            })).collect(Collectors.toList());

            //获取所有品牌id，过滤小米，苹果，华为的品牌id
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null).stream().filter(baseTrademark -> {
                return !tmIdList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());

            //返回数据
            return baseTrademarkList;
        }
        //当三级分类id下，没有品牌数据的时候，查询所有品牌数据
        return baseTrademarkMapper.selectList(null);
    }

    //保存品牌与分类的对应关系
    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //本质：向base_category_trademark表中插入数据
        //Long category3Id;   List<Long> trademarkIdList;
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        ArrayList<BaseCategoryTrademark> baseCategoryTrademarkArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            for (Long tmId : trademarkIdList) {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(tmId);
                baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
                baseCategoryTrademarkArrayList.add(baseCategoryTrademark);
            }

            //批量添加
            this.saveBatch(baseCategoryTrademarkArrayList);
        }
    }

    //根据三级分类id和品牌id，删除数据
    @Override
    public void remove(Long category3Id, Long trademarkId) {
        //本质：逻辑删除，修改is_deleted的值，，执行的是update语句，不是delete语句
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        queryWrapper.eq("trademark_id",trademarkId);
        baseCategoryTrademarkMapper.delete(queryWrapper);
    }
}
